rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library("readxl")
library("ggplot2")
library(gridExtra)
library(readstata13)
library(haven)
library(dplyr)
library(forcats)
library(ggeasy)
library(stargazer)

library(tidyverse)
library(broom)
library(ggplot2)
library(RDHonest)


cep_all <- read_dta("Data/cep_all.dta")

## Figure A1: Ideology outcome distribution

sample1 <- data.frame()
sample2 <- data.frame()
sample3 <- data.frame()
sample4 <- data.frame()
sample5 <- data.frame()


# Define the range of dosage values
dosage_ranges <- c(1, 2, 3, 4, 5)

# Loop through the dosage ranges and create samples
for (i in dosage_ranges) {
  subset_condition <- cep_all$treatment_dosage_coup >= -i & cep_all$treatment_dosage_coup <= i
  sample_name <- paste("sample", i, sep = "")
  
  # Store the samples in the respective data frame
  assign(sample_name, subset(cep_all, subset_condition))
}


sample5$Group <- ifelse(sample5$treatment_dosage_coup <= -1, "Control", 
                        ifelse(sample5$treatment_dosage_coup >= 1, "Treatment", "otherwise"))
sample5 <- sample5 %>%
  mutate(ideology_cat =
           case_when(
             left == 1 ~ "Left",
             right == 1 ~  "Right",
             none_pol == 1 ~ "None",
             center==1 ~ "Center",
           )) %>%
  mutate(ideology_cat=factor(ideology_cat)) %>% 
  mutate(ideology_cat=fct_relevel(ideology_cat,c("None","Left","Center","Right")))

figurea1 <- ggplot(filter(sample5, !is.na(ideology_cat)), aes(x=ideology_cat)) +
  geom_bar(aes(fill=Group)) +
  facet_wrap(~dob, scales = "free") + theme_minimal() +
  ylim(0,180) +
  scale_fill_manual(values=c("#E69F00", "steelblue")) +
  labs(x="Ideology",y="Count") +
  theme(legend.title=element_blank()) +
  theme(legend.text=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14)) +
  theme(axis.title.x = element_text(size = 14),  # Adjust the size of x-axis label
        axis.title.y = element_text(size = 14),  # Adjust the size of y-axis label
        axis.text.x = element_text(size = 14),   # Adjust the size of x-axis numbers
        axis.text.y = element_text(size = 14))   # Adjust the size of y-axis numbers

ggsave("Output/figurea1.pdf", plot = figurea1, width = 13, height = 11, units = "in")

print("Figure A1 complete")

## Figure A2: Democracy is preferable outcome distribution 1950 - 1960

cep_all <- read_dta("Data/cep_all.dta")

sample1 <- data.frame()
sample2 <- data.frame()
sample3 <- data.frame()
sample4 <- data.frame()
sample5 <- data.frame()

# Define the range of dosage values
dosage_ranges <- c(1, 2, 3, 4, 5)

# Loop through the dosage ranges and create samples
for (i in dosage_ranges) {
  subset_condition <- cep_all$treatment_dosage_coup >= -i & cep_all$treatment_dosage_coup <= i
  sample_name <- paste("sample", i, sep = "")
  
  # Store the samples in the respective data frame
  assign(sample_name, subset(cep_all, subset_condition))
}


sample5$Group <- ifelse(sample5$treatment_dosage_coup <= -1, "Control", 
                        ifelse(sample5$treatment_dosage_coup >= 1, "Treatment", "otherwise"))
sample5 <- sample5 %>%
  mutate(ideology_cat =
           case_when(
             left == 1 ~ "Left",
             right == 1 ~  "Right",
             none_pol == 1 ~ "None",
             center==1 ~ "Center",
           )) %>%
  mutate(ideology_cat=factor(ideology_cat)) %>% 
  mutate(ideology_cat=fct_relevel(ideology_cat,c("None","Left","Center","Right")))


figurea2 <- ggplot(filter(sample5, !is.na(dem_3)), aes(x=as.factor(dem_3))) +
  geom_bar(aes(fill=Group)) +
  facet_wrap(~dob, scales = "free") + theme_minimal() +
  scale_fill_manual(values=c("#E69F00", "steelblue")) +
  labs(x="Democracy is Always Preferable",y="Count") +
  theme(legend.title=element_blank())  +
  ylim(0,80) +
  theme(legend.text=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14)) +
  theme(axis.title.x = element_text(size = 14),  # Adjust the size of x-axis label
        axis.title.y = element_text(size = 14),  # Adjust the size of y-axis label
        axis.text.x = element_text(size = 14),   # Adjust the size of x-axis numbers
        axis.text.y = element_text(size = 14))   # Adjust the size of y-axis numbers

ggsave("Output/figurea2.pdf", plot = figurea2, width = 13, height = 11, units = "in")

print("Figure A2 complete")

# Figure A3: Democracy is functioning poorly outcome distribution 1950-1960

figurea3 <- ggplot(filter(sample5, !is.na(democracy_bad)), aes(x=as.factor(democracy_bad))) +
  geom_bar(aes(fill=Group)) +
  ylim(0,190) +
  facet_wrap(~dob, scale = "free") + theme_minimal() +
  scale_fill_manual(values=c("#E69F00", "steelblue")) +
  labs(x="Democracy Works Poorly",y="Count") +
  theme(legend.title=element_blank())  +
  theme(legend.text=element_text(size=14)) +
  theme(strip.text.x = element_text(size = 14)) +
  theme(axis.title.x = element_text(size = 14),  # Adjust the size of x-axis label
        axis.title.y = element_text(size = 14),  # Adjust the size of y-axis label
        axis.text.x = element_text(size = 14),   # Adjust the size of x-axis numbers
        axis.text.y = element_text(size = 14))   # Adjust the size of y-axis numbers

ggsave("Output/figurea3.pdf", plot = figurea3, width = 13, height = 11, units = "in")

print("Figure A3 complete")
